/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carrello.impl.controller.administrator;

import carrello.exception.CarrelloException;
import carrello.impl.controller.AbstractCTRL;
import carrello.model.association.AttributeValue;
import carrello.model.entities.Product;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import static carrello.util.EntityManagerUtil.getEntityManager;
/**
 *
 * @author Usman
 */
public class UpdateProductCTRL extends AbstractCTRL{
    public static LinkedList<LinkedList<String>> getAllAttributeValues ( int productid ) {
        Product p = Product.getProductById(productid);
        LinkedList<LinkedList<String>> atts = new LinkedList<LinkedList<String>> ();
        LinkedList<String> param = new LinkedList<String> ();
        param.add("Name");
        param.add(p.getName());
        param.add ("1");
        param.add("String");
        atts.add (param);
        
        param = new LinkedList<String> ();
        param.add ("Description");
        param.add (p.getDescription());
        param.add ("1");
        param.add("String");
        atts.add (param);
        
        param = new LinkedList<String> ();
        param.add ("Price");
        param.add (p.getPrice());
        param.add ("1");
        param.add("Double");
        atts.add (param);
        
        param = new LinkedList<String> ();
        param.add ("Manufacturer");
        param.add (p.getManufacturer());
        param.add ("1");
        param.add ("String");
        atts.add (param);
        
        param = new LinkedList<String> ();
        param.add ("Quantity");
        param.add (String.valueOf(p.getQuantity()));
        param.add ("1");
        param.add ("Integer");
        atts.add (param);
        
        param = new LinkedList<String> ();
        param.add ("CatalogId");
        param.add (String.valueOf(p.getCatalogId()));
        param.add ("1");
        param.add ("Integer");
        atts.add (param);
        
        Set<AttributeValue> values = p.getAttributeValue();
        for ( AttributeValue v : values ) {
            param = new LinkedList<String> ();
            param.add (v.getAttribute().getName());
            param.add (v.getValue());
            param.add (String.valueOf(v.getAttribute().getRequired()));
            param.add (v.getAttribute().getType());
            atts.add (param);
        }
        
        
        return atts;
    }

    public static void updateAttributeValues(String[] values, int id) throws CarrelloException {
        
        if ( Double.valueOf(values[2]) < 0.00 )
            throw new CarrelloException ("Constraint. Price has to be greater than or equal to zero");
        
        if ( Integer.parseInt(values[4]) < 0 )
            throw new CarrelloException ("Constraint. Quantity has to be greater than or equal to zero");

        Product p = Product.getProductById(id);
        
        p.setName (values[0]);
        p.setDescription(values[1]);
        p.setPrice(values[2]);
        p.setManufacturer(values[3]);
        p.setQuantity (Integer.parseInt(values[4]));
        p.setCatalogId(Integer.parseInt(values[5]));
        
        
        if ( values.length > 6 ) {
            int i = 6;
            Set<AttributeValue> set = p.getAttributeValue();
            Iterator<AttributeValue> it = set.iterator();
            while ( it.hasNext()) {
                AttributeValue v = it.next();
                v.setValue(values[i]);
                ++i;
            }
        }
        init();
        getEntityManager().update (p);
        end();
    }
    
}
